package com.web.onirigi.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.web.onirigi.model.ImageDisplayBean;
import com.web.onirigi.service.ImageDisService;
import com.web.onirigi.util.BaseResponseObject;
import com.web.onirigi.util.BaseUtil;
import com.web.onirigi.util.ParseJsonToMap;

/**
 * 轮播图展示DisPlayController
 * @author fengruiqi
 *
 */
@RestController
@RequestMapping("imageDisRest")
public class ImageDisplayRest {

	/**
	 * 日志组件
	 */
	private static final Logger logger =LoggerFactory.getLogger(ImageDisplayRest.class);
	
	/**
	 * 注入轮播图展示service
	 */
	@Autowired
	private ImageDisService  imageDisServiceImpl;
	
	/**
	 * 根据不同的类型值 展示不同的轮播图
	 * @param json
	 * @return
	 */
	@RequestMapping(value = "showImage",method=RequestMethod.POST)
	public BaseResponseObject   showImage(@RequestBody String json){
		logger.info("展示轮播图" + json);
		BaseResponseObject  object =new BaseResponseObject();
		String type =null;
		String RHtype= null;
		try {
			Map map= ParseJsonToMap.parseStringToMap(json);
			RHtype= (String) map.get("RHtype");
	//		type= map.get("type").toString();
			
	    	ImageDisplayBean imageBean =JSON.parseObject(JSON.toJSONString(map)
	    			,ImageDisplayBean.class );
		    	
	//    	if(BaseUtil.REST_IMAGE_TYPE_INDEX.equals(type)){
	    		
    		if(BaseUtil.REST_RHTYPE_ADD.equals(RHtype)) {
    			imageBean.setCreateDate(new Date());
    			// 暂时先这么设置 后台的话 。。 默认是管理员操作
    			imageBean.setOperateId(1);
    			imageDisServiceImpl.addImageDis(imageBean);
    		}else if(BaseUtil.REST_RHTYPE_UPDATE.equals(RHtype)) {
    			imageDisServiceImpl.updateImageDis(imageBean);
    			
    		}else if(BaseUtil.REST_RHTYPE_DELETE.equals(RHtype)) {
    			imageDisServiceImpl.deleteImageDis(imageBean);
    		}else {
    			List<ImageDisplayBean>  beans=imageDisServiceImpl.getImageDis(imageBean);
    			Map resultMap =new HashMap<>();
    			resultMap.put("result", new PageInfo<>(beans));
    			object.setResponseData(resultMap);
    			
    		}
    		object.setResponseCode(BaseUtil.SUCCESS_CODE);
    		object.setResponseMessage(BaseUtil.IMAGE_DISPLAY_SUCCENT_CONTENT);
	//    	}
		
		} catch (Exception e) {
			object.setResponseCode(BaseUtil.FAILED_CODE);
			object.setResponseMessage(BaseUtil.IMAGE_DISPLAY_FAILED_CONTENT);
			logger.error("展示轮播图出现错误,展示类型：{}"+type,e);
		}
		
		
		return object;
		
		
	}
}
